#! /usr/bin/python3
import requests
from lxml import etree
import pandas as pd
import operator
import time
import random
import ast
import sys


url_yuce_lianzhong = {}
def get_ding5_html_data(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    r1 = requests.get(url, headers=headers)
    parse_html = etree.HTML(r1.text)
    #s_yu_list = parse_html.xpath('/html/body/section/div/div/div/div/div/span/text()')
    ok_list = parse_html.xpath('/html/body/section/div/div/div/table/tr/td/span/text()')

    #print(ok_list)
    #连续预测对       5(0) 6(1) 7(2) 8(3) 9(4) 10(5)
    #连续预测错       2(6) 3(7) 4(8) 5(9) 6(10)
    # 8 17 26 35 44 53 62 71 80 89
    tmp_ok_conunt = [0,0,0,0,0,0,0,0,0,0,0]
    if ok_list[8] != '命中' and ok_list[16] != '命中':
        tmp_ok_conunt[6] = 1

    if ok_list[8] != '命中' and ok_list[16] != '命中' and \
        ok_list[24] != '命中' :
        tmp_ok_conunt[7] = 1

    if ok_list[8] != '命中' and ok_list[16] != '命中' and \
        ok_list[24] != '命中' and ok_list[32] !='命中':
        tmp_ok_conunt[8] = 1

    if ok_list[8] != '命中' and ok_list[16] != '命中' and \
        ok_list[24] != '命中' and ok_list[32] !='命中' and \
        ok_list[40] != '命中':
        tmp_ok_conunt[9] = 1

    if ok_list[8] == '命中' and ok_list[17] == '命中' and \
        ok_list[26] == '命中' and ok_list[35] == '命中' and \
        ok_list[44] == '命中':
        tmp_ok_conunt[0] = 1

    if ok_list[8] == '命中' and ok_list[17] == '命中' and \
        ok_list[26] == '命中' and ok_list[35] == '命中' and \
        ok_list[44] == '命中' and ok_list[53] == '命中':
        tmp_ok_conunt[1] = 1

    if ok_list[8] == '命中' and ok_list[17] == '命中' and \
        ok_list[26] == '命中' and ok_list[35] == '命中' and \
        ok_list[44] == '命中' and ok_list[53] == '命中' and \
        ok_list[62] == '命中':
        tmp_ok_conunt[2] = 1

    if ok_list[8] == '命中' and ok_list[17] == '命中' and \
        ok_list[26] == '命中' and ok_list[35] == '命中' and \
        ok_list[44] == '命中' and ok_list[53] == '命中' and \
        ok_list[62] == '命中' and ok_list[71] == '命中':
        tmp_ok_conunt[3] = 1

    if ok_list[8] == '命中' and ok_list[17] == '命中' and \
        ok_list[26] == '命中' and ok_list[35] == '命中' and \
        ok_list[44] == '命中' and ok_list[53] == '命中' and \
        ok_list[62] == '命中' and ok_list[71] == '命中' and \
        ok_list[80] == '命中':
        tmp_ok_conunt[4] = 1


    url_yuce_lianzhong[url] = tmp_ok_conunt
    print(url_yuce_lianzhong[url])

def get_url_experts():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    s_ge = "https://app.ydniu.com/p5/expert_list_个位杀3码"
    s_shi = "https://app.ydniu.com/p5/expert_list_十位杀3码"
    s_bai = "https://app.ydniu.com/p5/expert_list_百位杀3码"
    s_qian = "https://app.ydniu.com/p5/expert_list_千位杀3码"
    s_wan = "https://app.ydniu.com/p5/expert_list_万位杀3码"

    d_wan = 'https://app.ydniu.com/p5/expert_list_万位定五'
    d_qian = 'https://app.ydniu.com/p5/expert_list_千位定五'
    d_bai = 'https://app.ydniu.com/p5/expert_list_百位定五'
    d_shi = 'https://app.ydniu.com/p5/expert_list_十位定五'
    d_ge = 'https://app.ydniu.com/p5/expert_list_个位定五'

    ex_list = [s_ge,s_shi, s_bai, s_qian, s_wan,\
               d_ge, d_shi, d_bai, d_qian, d_wan]
    ex_url = []
    for k in ex_list:
        url = k
        r1 = requests.get(url, headers=headers)
        parse_html = etree.HTML(r1.text)
        url_base = "https://app.ydniu.com"
        experts_list = parse_html.xpath('/html/body/section/div/a/@href')
        #print(experts_list)
        for i in experts_list:
            tmp = f"{url_base}{i}"
            #print(tmp)
            if tmp[32:40] not in ex_url:
                ex_url.append(tmp[32:40])

    print(len(ex_url))
    #print(ex_url)
    return ex_url

def build_total_url_list(ex_list):
    base_url = 'https://app.ydniu.com/p5/expert-'
    list_01 = ["-万位杀3码","-千位杀3码","-百位杀3码","-十位杀3码"]
    total_url_ex = []
    for i in list_01:
        tmp01 = []
        for j in ex_list:
            tmp = f"{base_url}{j}{i}"
            tmp01.append(tmp)
        total_url_ex.append(tmp01)
    #print(total_url_ex)
    return total_url_ex

def build_wangzhan_url():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    s_ge = "https://app.ydniu.com/p5/expert_list_个位杀3码"
    s_shi = "https://app.ydniu.com/p5/expert_list_十位杀3码"
    s_bai = "https://app.ydniu.com/p5/expert_list_百位杀3码"
    s_qian = "https://app.ydniu.com/p5/expert_list_千位杀3码"
    s_wan = "https://app.ydniu.com/p5/expert_list_万位杀3码"

    d_wan = 'https://app.ydniu.com/p5/expert_list_万位定五'
    d_qian = 'https://app.ydniu.com/p5/expert_list_千位定五'
    d_bai = 'https://app.ydniu.com/p5/expert_list_百位定五'
    d_shi = 'https://app.ydniu.com/p5/expert_list_十位定五'
    d_ge = 'https://app.ydniu.com/p5/expert_list_个位定五'
    ex_list = [s_shi, s_bai, s_qian, s_wan]
    ex_url = []
    all_ex_20 = []
    for k in ex_list:
        url = k
        r1 = requests.get(url, headers=headers)
        parse_html = etree.HTML(r1.text)
        url_base = "https://app.ydniu.com"
        experts_list = parse_html.xpath('/html/body/section/div/a/@href')
        #print(experts_list)
        tmp_list = []
        for i in experts_list:
            tmp = f"{url_base}{i}"
            #print(tmp)
            tmp_list.append(tmp)
        all_ex_20.append(tmp_list)
    #print(all_ex_20)
    return all_ex_20

def get_ding5_data(url):
    s_yu_list = ['1','2','3','4','5']
    return s_yu_list
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    r1 = requests.get(url, headers=headers)
    parse_html = etree.HTML(r1.text)
    s_yu_list = parse_html.xpath('/html/body/section/div/div/div/div/div/span/text()')
    return s_yu_list


def show_data(flag, conut_cc, list_01):
    code_count = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, \
                  '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
    code = ''
    count = ''
    for i in list_01:
        if i == ' ':
            continue
        else:
            code_count[i] = code_count[i] + 1

    for key in code_count:
        code = code + "\t" + str(key)
        count = count + "\t" + str(code_count[key])

    print(f"--[ 杀{flag} ]----[ {conut_cc} ]--")
    print(code)
    print(count)

def get_yuce_data():
    with open('./sha_total_ex_l_data', 'r') as f:
        all_20_expert_str = f.readline()
    all_20_expert_dict = ast.literal_eval(all_20_expert_str)
    #print(all_20_expert_dict)
    #print(type(all_20_expert_dict))
    #连续预测对       5(0) 6(1) 7(2) 8(3) 9(4) 10(5)
    #连续预测错       2(6) 3(7) 4(8) 5(9) 6(10)

    jishu = [0, 0, 0, 0]
    shi_ = ''
    bai_ = ''
    qian_ = ''
    wan_ = ''

    for key in all_20_expert_dict:
        if "十位" in key:
            if all_20_expert_dict[key][flag_conut] == 1:
                #print(flag_conut)
                jishu[0] = jishu[0] + 1
                tmp_data = get_ding5_data(key)
                str_temp = "".join(tmp_data)
                shi_ = shi_ + str_temp
                #print(all_20_expert_dict[key])

        if "百位" in key:
            if all_20_expert_dict[key][flag_conut] == 1:
                jishu[1] = jishu[1] + 1
                tmp_data = get_ding5_data(key)
                str_temp = "".join(tmp_data)
                bai_ = bai_ + str_temp
                #print(all_20_expert_dict[key])
        if "千位" in key:
            if all_20_expert_dict[key][flag_conut] == 1:
                jishu[2] = jishu[2] + 1
                tmp_data = get_ding5_data(key)
                str_temp = "".join(tmp_data)
                qian_ = qian_ + str_temp
                #print(all_20_expert_dict[key])
        if "万位" in key:
            if all_20_expert_dict[key][flag_conut] == 1:
                jishu[3] = jishu[3] + 1
                tmp_data = get_ding5_data(key)
                str_temp = "".join(tmp_data)
                wan_ = wan_ + str_temp
                #print(all_20_expert_dict[key])

    shi_l = list(shi_)
    shi_l.sort()
    bai_l = list(bai_)
    bai_l.sort()
    qian_l = list(qian_)
    qian_l.sort()
    wan_l = list(wan_)
    wan_l.sort()

    print("原始数据：", shi_)
    show_data('十位', jishu[0], shi_l)
    print("原始数据：", bai_)
    show_data('百位', jishu[1], bai_l)
    print("原始数据：", qian_)
    show_data('千位', jishu[2], qian_l)
    print("原始数据：", wan_)
    show_data('万位', jishu[3], wan_l)

flag_conut = 1

if __name__ == "__main__":
    print("start...")
    ex_l = get_url_experts()
    ex_l = build_total_url_list(ex_l)
    print(ex_l)
    for i in ex_l:
        for j in i:
            print(j)
            get_ding5_html_data(j)
    print("------------------------------")
    print(url_yuce_lianzhong)
    name_ = '/home/ubuntu/ydn/sha/sha_total_ex_l_data'
    with open(name_, 'w') as f:
        f.write(str(url_yuce_lianzhong))
    #
    #get_ding5_ng_html_data(url)
